<?php

namespace app\api\model\music;

use app\logic\model\music\MusicDjLogicModel;
use app\logic\model\music\MusicLogicModel;
use app\logic\model\user\UserLogicModel;
use app\logic\single\MusicSingle;
use app\logic\viewModel\MusicUserView;

class MusicDjApiModel extends MusicDjLogicModel
{

    public function getSelect(array $where=[], array $field=[], string $order='id desc', int $limit=15){
        return $this->where($where)
            ->field($field[0]?:'*')
            ->order($order)
            ->withJoin(['music'])
            ->visible([
                'music' => $field[1]??['*'],
            ])
            ->limit($limit)
            ->select();
    }

    public function getToday(array $where, array $field, string $string, int $int,string $order='id desc',int $limit=15)
    {
        return $this->where($where)
            ->field($field[0]?:'*')
            ->order($order)
            ->withJoin(['music'])
            ->visible([
                'music' => $field[1]??['*'],
            ])
            ->limit($limit)
            ->select();
    }

    public function getApiList(array $param, array $where=[], string $field='*', $page=1, $limit=15,string $order='sort desc')
    {
        $query=$this->where($where)
            ->where(function ($q) use ($param){
                if(check_request($param,'title')){
                    $music_id_arr = MusicLogicModel::getIdsByTitle($param['title'])?:[-1];
                }
                if(check_request($param,'type_id')){
                    $q->whereIn("type_id",$param['type_id']);
                }
                if(check_request($param,'writer_id')){
                    $music_id_arr = MusicLogicModel::getIdsByUserId($param['writer_id'])?:[-1];
                }
                if(!empty($music_id_arr)){
                    $q->whereIn("music_id",array_unique($music_id_arr));
                }
            })
            ->with(['music'])->visible(
                ['music'=>['title','url']]
            );
        $list = $query->field($field)->order($order)
            ->page($page,$limit)->select();
        $count = $query->count();
        if($count>0){
            $list = $this->getWriterByMusic($list->toArray());
        }
        return [$list,$count];
    }


}